

#include "iostream"
#include "vector"
#include "unordered_map"
#include "unordered_set"
#include "algorithm"
#include "string"
#include "list"
#include "string.h"

class OneLoop {
public:
    std::list<int> queue;
    std::unordered_map<int,std::list<int>::iterator> search;
    bool run(int a,int b
    ) {
        for(int i=0;i<a;i++){
            int in;std::cin>>in;
            queue.push_back(in);
            auto iter=queue.end();
            iter--;
            search[in]=iter;
        }
        for(int j=0;j<b;j++){
            char cmd;
            while(1){
                cmd=std::cin.get();
                if(cmd=='\n'){
                    break;
                }
            }
            for (int i = 0; i < 3; ++i) {
                cmd=std::cin.get();
            }
            if(cmd=='L'){
                int m;
                std::cin>>m;
                queue.erase(search[m]);
                search.erase(m);
            }else{
                int m,n;
                std::cin>>m>>n;
                auto insat=search[m];
                auto ins=queue.insert(insat,n);
                search[n]=ins;
            }
        }
        printf("%d\n",queue.size());
        for(auto q:queue){
            printf("%d ",q);
        }
        return true;
    }
};

class Solution {
public:
    void run() {

        bool first = true;
        int a, b, c, d, e;
//        std::cin >> a;

while (std::cin>>a>>b) {
            OneLoop l;
            if (!l.run(a,b)) {
//                return;
            }
        }


    }
};

int main() {
//    bool cur_num= true;
    Solution().run();
    return 0;
}

